package com.bjpn.workbench.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpn.settings.bean.User;
import com.bjpn.settings.mapper.UserMapper;
import com.bjpn.workbench.bean.Activity;
import com.bjpn.workbench.bean.ActivityLike;
import com.bjpn.workbench.mapper.ActivityMapper;
import com.bjpn.workbench.service.ActivityService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2022-09-06
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int saveActivity(Activity activity) {
        return activityMapper.insert(activity);
    }

    @Override
    public List<Activity> getActivityByPageService(Integer pageNum, Integer pageSize, ActivityLike al) {
        String activityLikeName = al.getActivityLikeName();
        String activityLikeOwner = al.getActivityLikeOwner();
        String activityLikeStartTime = al.getActivityLikeStartTime();
        String activityLikeEndTime = al.getActivityLikeEndTime();
        QueryWrapper<Activity> wrapper = Wrappers.query();
        if (activityLikeName != null && !"".equals(activityLikeName)) {

            wrapper.like("activity_name", activityLikeName);
        }
        if (activityLikeOwner != null && !"".equals(activityLikeOwner)) {
            QueryWrapper<User> userWrapper = Wrappers.query();
            // 模糊名查到所有满足用户
            List<User> userList = userMapper.selectList(userWrapper.like("user_name", activityLikeOwner));
            for (User user : userList) {
                // 将所有userId传给activityOwner
                wrapper.in("activity_owner", user.getUserId());
            }
        }
        if (activityLikeStartTime != null && !"".equals(activityLikeStartTime)) {
            wrapper.like("activity_start_date", activityLikeStartTime);
        }
        if (activityLikeEndTime != null && !"".equals(activityLikeEndTime)) {
            wrapper.like("activity_end_date", activityLikeEndTime);
        }

        // 告诉拦截器需要分页 不需要count因为配置文件开启
        // 二者一定要紧邻 先后顺序
        PageHelper.startPage(pageNum, pageSize);
        return activityMapper.selectList(wrapper);
    }

    @Override
    public List<Activity> getAllActivity() {
        return activityMapper.selectList(null);
    }
}
